package topic.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;
import com.makeramen.roundedimageview.RoundedImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import in.srain.cube.views.ptr.PtrFrameLayout;
import topic.adapter.SecondCommentAdapter;
import topic.bean.SecondCommentBean;
import https.InterNetController;
import topic.bean.TopicDetailBean;
import personalPage.activity.PersonalPage;
import view.MyImageLoader;
import utils.MyLogUtils;
import utils.VariousUtils;
import view.MyConfirmDialog;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/3/24.
 * 一级评论首页,二级评论列表
 */
@ContentView(R.layout.second_comment)
public class SecondComment extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.second_comment_back)
    private ImageView second_comment_back;
    @ViewInject(R.id.second_comment_refresh_layout)
    private MyPtrRefresh_LoadView second_comment_refresh_layout;
    private TextView second_comment_name;
    private TextView second_comment_time;
    private TextView second_comment_content;
    private TextView second_comment_header_delete;
    private RoundedImageView second_comment_ima;
    @ViewInject(R.id.second_comment_lv)
    private ListView second_comment_lv;
    @ViewInject(R.id.second_comment_edit_layout)
    private RelativeLayout second_comment_edit_layout;
    //    @ViewInject(R.id.second_comment_share_ima)
//    private ImageView second_comment_share_ima;
    private View headerView;

    private int page = 1;
    private String blogid;
    private SecondCommentAdapter secondCommentAdapter;
    private TopicDetailBean.CommentListBean commentListBean;
    private ArrayList<SecondCommentBean.CommentBean.RecommentListBean> arrayList;

    private final int ONREFRESH = 0;
    private final int ONLOAD = 1;
    private final int DELETE = 2;

    private boolean firstOpen;
    private boolean hasComment;

    private static final String TAG = "SecondComment";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    if (msg.arg1 == DELETE) {
                        MyLogUtils.e(TAG, "删除成功");
                        hasComment = true;
                        Intent intent = new Intent();
                        intent.putExtra("hasComment", hasComment);
                        SecondComment.this.setResult(2, intent);
                        SecondComment.this.finish();
                    } else {
                        Gson gson = new Gson();
                        SecondCommentBean secondCommentBean = gson.fromJson((String) msg.obj, SecondCommentBean.class);
                        if (secondCommentBean != null) {
                            switch (msg.arg1) {
                                case ONREFRESH:
                                    second_comment_name.setText(secondCommentBean.getComment().getNickname());
                                    second_comment_time.setText(secondCommentBean.getComment().getCreate_time());
                                    second_comment_content.setText(secondCommentBean.getComment().getContent());
                                    MyImageLoader.showImage(secondCommentBean.getComment().getHeadimgurl(), second_comment_ima);
                                    if (!VariousUtils.isSamId(secondCommentBean.getComment().getUser_id())) {
                                        //可以评论
                                        second_comment_edit_layout.setVisibility(View.VISIBLE);
                                    } else {
                                        second_comment_edit_layout.setVisibility(View.GONE);
                                    }
                                    if (secondCommentBean.getComment().getShowdelete() == 0) {
                                        //隐藏删除
                                        second_comment_header_delete.setVisibility(View.GONE);
                                    } else {
                                        //显示删除
                                        second_comment_header_delete.setVisibility(View.VISIBLE);
                                    }
                                    arrayList.clear();
                                    break;
                                case ONLOAD:
                                    break;
                                default:
                                    break;
                            }
                            List<SecondCommentBean.CommentBean.RecommentListBean> recommentList = secondCommentBean.getRecommentList();
                            if (recommentList != null) {
                                if (recommentList.size() > 0) {
                                    for (int i = 0; i < recommentList.size(); i++) {
                                        arrayList.add(recommentList.get(i));
                                    }
                                }
                                secondCommentAdapter.notifyDataSetChanged();

                            }
                        }
                        second_comment_refresh_layout.OnRefreshComplete();
                        firstOpen = false;
                    }
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        blogid = getIntent().getStringExtra("blogid");
        commentListBean = (TopicDetailBean.CommentListBean) getIntent().getSerializableExtra("CommentListBean");
        initView();
        firstOpen = true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (firstOpen) {
            getDataFromInterNet(page = 1, ONREFRESH, true);
        } else {
            getDataFromInterNet(page, ONREFRESH, false);
        }
    }

    private void initView() {
        headerView = LayoutInflater.from(this).inflate(R.layout.second_comment_header, null);
        second_comment_name = (TextView) headerView.findViewById(R.id.second_comment_header_name);
        second_comment_content = (TextView) headerView.findViewById(R.id.second_comment_header_content);
        second_comment_ima = (RoundedImageView) headerView.findViewById(R.id.second_comment_header_head_ima);
        second_comment_time = (TextView) headerView.findViewById(R.id.second_comment_header_time);
        second_comment_header_delete = (TextView) headerView.findViewById(R.id.second_comment_header_delete);
        second_comment_header_delete.setOnClickListener(this);
        second_comment_ima.setOnClickListener(this);
        second_comment_name.setOnClickListener(this);
        arrayList = new ArrayList<>();
        second_comment_lv.addHeaderView(headerView);
        secondCommentAdapter = new SecondCommentAdapter(this, blogid, arrayList);
        second_comment_lv.setAdapter(secondCommentAdapter);
        second_comment_back.setOnClickListener(this);
        second_comment_edit_layout.setOnClickListener(this);
//        second_comment_share_ima.setOnClickListener(this);
        second_comment_refresh_layout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, ONLOAD, false);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, ONREFRESH, false);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent i = new Intent();
        switch (v.getId()) {
            case R.id.second_comment_back:
                this.finish();
                break;
            case R.id.second_comment_header_head_ima:
            case R.id.second_comment_header_name:
                //进入个人主页
                i.setClass(SecondComment.this, PersonalPage.class);
                i.putExtra("userId", commentListBean.getUser_id());
                i.putExtra("type", "app");
                startActivity(i);
                break;

            case R.id.second_comment_edit_layout:
                if (hasLoad()) {
                    i.setClass(SecondComment.this, DeliverComment.class);
                    i.putExtra("blogid", blogid);
                    i.putExtra("commentid", commentListBean.getComment_id());
                    startActivity(i);
                } else {
                    login(this);
                }
                break;
            case R.id.second_comment_header_delete:
                //删除
                final MyConfirmDialog confirmDialog = new MyConfirmDialog(this, R.style.confirmDialog, "确认删除?");
                confirmDialog.setCancelListener(new MyConfirmDialog.CancelListener() {
                    @Override
                    public void cancel() {
                        confirmDialog.dismiss();
                    }
                });
                confirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
                    @Override
                    public void confirm() {
                        deleteComment(commentListBean.getComment_id());
                        confirmDialog.dismiss();
                    }
                });
                confirmDialog.show();
                break;
            default:
                break;
        }
    }

    private void getDataFromInterNet(int page, int type, boolean waitingDialog) {
        //获取评论列表
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("blogid", blogid);
        hashMap.put("comment_id", commentListBean.getComment_id());
        hashMap.put("page", String.valueOf(page));
        new InterNetController(this, Constant.SECONDCOMMENT, handler, hashMap, type, waitingDialog);
    }

    private void deleteComment(String commentId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("comment_id", commentId);
        new https.InterNetController(this, Constant.DELETECOMMENT, handler, hashMap, DELETE);
    }

}
